<template>
    <div>
    <el-form inline class="demo-form-inline">
    <el-row >
    <el-col :span="6">
        <el-form-item label="任务名称">
            <el-input v-model="query.payload.name" placeholder="任务名称" />
        </el-form-item>
    </el-col>
    <el-col :span="6">
        <el-form-item label="任务编码">
            <el-input v-model="query.payload.code" placeholder="任务编码" />
        </el-form-item>
    </el-col>
    <el-col :span="6">
        <el-form-item label="备注">
            <el-input v-model="query.payload.remark" placeholder="备注" />
        </el-form-item>
    </el-col>
    <el-col :span="6">
        <el-form-item label="状态">
            <el-select v-model="query.payload.status" placeholder="请选择">
                <el-option  label="开启" :value = 1 />
                <el-option  label="关闭" :value = 0 />
            </el-select>
        </el-form-item>

    </el-col>
    </el-row>
    <el-row >
        <el-form-item label="时间范围">
            <el-date-picker
                    v-model="query.dateRange"
                    type="datetimerange"
                    range-separator="到"
                    start-placeholder="开始时间"
                    end-placeholder="结束时间"
            />
        </el-form-item>
        <el-form-item>
            <el-button type="primary" activity @click="queryJob">查询</el-button>
            <el-button type="warning" activity @click="reset">重置</el-button>
            <el-button type="success" @click="addJob">新增</el-button>
        </el-form-item>
    </el-row>
    </el-form>
    <el-divider style="margin: 12px 0"/>
    <el-table ref="table" border v-loading="loading"  :data="data.tableData" header-cell-class-name="headerClass">
        <el-table-column align="center" type="index"  label="序号" width="100px" />
        <el-table-column align="center" prop="name" label="任务名称" />
        <el-table-column align="center" prop="code" label="任务编码" />
        <el-table-column align="center" prop="remark" label="备注" />
        <el-table-column align="center" prop="status" label="状态" >
            <template v-cloak #default="scope">
                {{scope.row.status == 1 ? '开启':'关闭'}}
            </template>
        </el-table-column>
        <el-table-column align="center"  prop="updateTime" label="更新时间" >
            <template #default="scope">
                {{moment(scope.row.updateTime).format("yyyy-MM-DD HH:mm:ss")}}
            </template>
        </el-table-column>
        <el-table-column align="center"  label="操作">
            <template #default="scope">
                <el-popconfirm title="手动执行该任务？" @confirm="execJob(scope.row.code)"  @cancel="">
                    <template #reference>
                        <el-link title="执行" type="primary">
                            <el-icon :size="15">
                                <Refresh />
                            </el-icon>
                            执行
                        </el-link>
                    </template>
                </el-popconfirm>
                &nbsp;
                <el-link title="修改" type="primary" @click="editJob(scope.row.id)">
                    <el-icon :size="15">
                        <Edit />
                    </el-icon>
                    修改
                </el-link>
                &nbsp;
                <el-popconfirm title="删除？" @confirm="deleteJob(scope.row.id)"  @cancel="">
                    <template #reference>
                        <el-link type="danger" title="删除">
                            <el-icon :size="15">
                                <Delete />
                            </el-icon>
                            删除
                        </el-link>
                    </template>
                </el-popconfirm>
            </template>
        </el-table-column>
</el-table>
<el-pagination
        style="display: flex;align-items:center;justify-content:center;margin-top: 10px"
        v-model:currentPage="query.currentPage"
        v-model:page-size="query.pageSize"
        :page-sizes="[10, 20, 30]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="query.total"
        @size-change="queryJob"
        @current-change="queryJob"
/>
<ModJob @close="data.modJob = false"  @reload="queryJob" :modJob="data.modJob" :type="data.type" :id="data.id" />
</div>
</template>

<script setup>
    import ModJob from '@/components/system/ModJob.vue'
    import request from '@/utils/request.js'
    import { ElMessage } from 'element-plus'
    import {ref, reactive, onMounted} from 'vue'
    import moment from 'moment'

    let data = reactive({
        modRole:false,
        roleToMenu:false,
        tableData:[],
        type:"",
        id:"",
    });
    let loading = ref(true)
    let query = reactive({dateRange:[],payload:{}, currentPage:1,pageSize:10,total:0})
    onMounted(() => {
        queryJob()
    })
    function queryJob(){
        query.startTime = query.dateRange[0]
        query.endTime = query.dateRange[1]
        request.post("/api/job/getJobPage", query).then(resp => {
            data.tableData = resp.records
            query.total = resp.total
            loading.value = false
        })
    }

    function reset(){
            query.payload.name = ""
            query.payload.code = ""
            query.payload.remark = ""
            query.payload.status = ""
    }

    function addJob(){
        data.modJob = true
        data.type = "add"
    }

    function deleteJob(id){
        request.get(`/api/job/delete/${id}`).then(resp => {
            queryJob()
            ElMessage({message: '删除成功！',type: 'success'})
        });
    }
    function execJob(code){
        request.get(`/api/job/exec/${code}`).then(resp => {
            queryJob()
            ElMessage({message: `开始执行执行${code}！`, type: 'success'})
        });
    }

    function editJob(id){
        data.id = id
        data.type = "edit"
        data.modJob = true
    }

</script>
<style lang="css">

</style>


